/*
 * ClientFRame.java
 *
 * Created on 10 luglio 2007, 12.04
 */

package bhclient;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import javax.swing.DefaultListModel;

import org.jopac2.jbal.RecordFactory;
import org.jopac2.jbal.RecordInterface;
import org.jopac2.utils.BookSignature;

import bookhive.client.ClientLauncher;
import bookhive.client.ClientPeer;
import bookhive.client.ResultListener;
import bookhive.datastructures.catalog.CatalogGroupInfo;
import bookhive.datastructures.datasource.DataItem;
import bookhive.datastructures.datasource.HashItem;

/**
 * 
 * @author Iztok
 */
public class ClientFrame extends javax.swing.JFrame implements ResultListener {

	private static final long serialVersionUID = 3399413272955764265L;

	ClientLauncher cl = null;

	ClientPeer cp = null;

	TreeMap<String, TreeSet<BookSignature>> hashSigns = null;

	TreeMap<String, RecordInterface> hashData = null;

	TreeMap<String, String> viewRiHash = null;

	TreeMap<String, TreeSet<BookSignature>> hashSources = null;

	DefaultListModel signslistmodel;

	DefaultListModel datalistmodel;

	DefaultListModel sourcelistmodel;

	boolean connected = false;

	private String currentHash = "";

	/** Creates new form ClientFRame */
	public ClientFrame() {

		signslistmodel = new DefaultListModel();
		datalistmodel = new DefaultListModel();
		sourcelistmodel = new DefaultListModel();
		initComponents();
		hashSigns = new TreeMap<String, TreeSet<BookSignature>>();
		hashData = new TreeMap<String, RecordInterface>();
		viewRiHash = new TreeMap<String, String>();
		hashSources = new TreeMap<String, TreeSet<BookSignature>>();
		cl = new ClientLauncher(this);
		cl.start();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated
	// Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		AddSignForm = new javax.swing.JFrame();
		jLabel5 = new javax.swing.JLabel();
		recordArea = new javax.swing.JScrollPane();
		SignFrom_RecordArea = new javax.swing.JTextArea();
		jLabel6 = new javax.swing.JLabel();
		SignFrom_LibIDField = new javax.swing.JTextField();
		jLabel7 = new javax.swing.JLabel();
		SignFrom_LibNameField = new javax.swing.JTextField();
		jLabel8 = new javax.swing.JLabel();
		SignFrom_BookNumField = new javax.swing.JTextField();
		jLabel9 = new javax.swing.JLabel();
		SignFrom_BookLocField = new javax.swing.JTextField();
		jLabel10 = new javax.swing.JLabel();
		SignFrom_BookConsField = new javax.swing.JTextField();
		SignFrom_AddButton = new javax.swing.JButton();
		SignForm_HashArea = new javax.swing.JTextField();
		SignForm_ClearButton = new javax.swing.JButton();
		OkBox = new javax.swing.JDialog();
		OkMessage = new javax.swing.JLabel();
		OkButton = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		groupNameField = new javax.swing.JTextField();
		connectButton = new javax.swing.JButton();
		MainTab = new javax.swing.JPanel();
		AUTLabel = new javax.swing.JLabel();
		TITLabel = new javax.swing.JLabel();
		AUTField = new javax.swing.JTextField();
		TITField = new javax.swing.JTextField();
		searchButton = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		LogArea = new javax.swing.JTextArea();
		jScrollPane2 = new javax.swing.JScrollPane();
		resultDataList = new javax.swing.JList();
		jScrollPane3 = new javax.swing.JScrollPane();
		signsList = new javax.swing.JList();
		jScrollPane4 = new javax.swing.JScrollPane();
		ISBDArea = new javax.swing.JTextArea();
		jLabel4 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		addSignButton = new javax.swing.JButton();
		RDVLabel = new javax.swing.JLabel();
		RDVField = new javax.swing.JTextField();

		AddSignForm
				.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		AddSignForm.setTitle("Add Signature");
		AddSignForm.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				AddSignFormWindowClosing(evt);
			}
		});

		jLabel5.setText("Add signature to record:");

		SignFrom_RecordArea.setColumns(20);
		SignFrom_RecordArea.setEditable(false);
		SignFrom_RecordArea.setLineWrap(true);
		SignFrom_RecordArea.setRows(5);
		SignFrom_RecordArea.setMargin(new java.awt.Insets(1, 4, 1, 8));
		recordArea.setViewportView(SignFrom_RecordArea);

		jLabel6.setText("Library Id:");

		jLabel7.setText("Library Name:");

		jLabel8.setText("Book Number:");

		jLabel9.setText("Book Localization:");

		jLabel10.setText("Book Consistency:");

		SignFrom_AddButton.setText("Add");
		SignFrom_AddButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				SignFrom_AddButtonMouseClicked(evt);
			}
		});

		SignForm_HashArea.setEditable(false);
		SignForm_HashArea.setMargin(new java.awt.Insets(1, 4, 1, 1));

		SignForm_ClearButton.setText("Clear");
		SignForm_ClearButton
				.addMouseListener(new java.awt.event.MouseAdapter() {
					public void mouseClicked(java.awt.event.MouseEvent evt) {
						SignForm_ClearButtonMouseClicked(evt);
					}
				});

		org.jdesktop.layout.GroupLayout AddSignFormLayout = new org.jdesktop.layout.GroupLayout(
				AddSignForm.getContentPane());
		AddSignForm.getContentPane().setLayout(AddSignFormLayout);
		AddSignFormLayout
				.setHorizontalGroup(AddSignFormLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								AddSignFormLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												AddSignFormLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																AddSignFormLayout
																		.createParallelGroup(
																				org.jdesktop.layout.GroupLayout.LEADING)
																		.add(
																				AddSignFormLayout
																						.createSequentialGroup()
																						.add(
																								AddSignFormLayout
																										.createParallelGroup(
																												org.jdesktop.layout.GroupLayout.LEADING)
																										.add(
																												jLabel5)
																										.add(
																												AddSignFormLayout
																														.createSequentialGroup()
																														.add(
																																AddSignFormLayout
																																		.createParallelGroup(
																																				org.jdesktop.layout.GroupLayout.LEADING)
																																		.add(
																																				jLabel6)
																																		.add(
																																				jLabel7)
																																		.add(
																																				jLabel8)
																																		.add(
																																				jLabel9)
																																		.add(
																																				jLabel10))
																														.add(
																																36,
																																36,
																																36)
																														.add(
																																AddSignFormLayout
																																		.createParallelGroup(
																																				org.jdesktop.layout.GroupLayout.LEADING,
																																				false)
																																		.add(
																																				SignFrom_BookConsField)
																																		.add(
																																				org.jdesktop.layout.GroupLayout.TRAILING,
																																				SignFrom_BookLocField)
																																		.add(
																																				SignFrom_BookNumField)
																																		.add(
																																				SignFrom_LibNameField)
																																		.add(
																																				SignFrom_LibIDField,
																																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																																				277,
																																				Short.MAX_VALUE))
																														.addPreferredGap(
																																org.jdesktop.layout.LayoutStyle.RELATED,
																																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																																Short.MAX_VALUE)))
																						.addContainerGap())
																		.add(
																				org.jdesktop.layout.GroupLayout.TRAILING,
																				AddSignFormLayout
																						.createSequentialGroup()
																						.add(
																								AddSignFormLayout
																										.createParallelGroup(
																												org.jdesktop.layout.GroupLayout.TRAILING)
																										.add(
																												org.jdesktop.layout.GroupLayout.LEADING,
																												SignForm_HashArea,
																												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																												401,
																												Short.MAX_VALUE)
																										.add(
																												recordArea,
																												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																												401,
																												Short.MAX_VALUE))
																						.addContainerGap()))
														.add(
																org.jdesktop.layout.GroupLayout.TRAILING,
																AddSignFormLayout
																		.createSequentialGroup()
																		.add(
																				SignForm_ClearButton)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				SignFrom_AddButton)
																		.addContainerGap()))));
		AddSignFormLayout
				.setVerticalGroup(AddSignFormLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								AddSignFormLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(jLabel5)
										.add(4, 4, 4)
										.add(
												SignForm_HashArea,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												recordArea,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												117,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												AddSignFormLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel6)
														.add(
																SignFrom_LibIDField,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												AddSignFormLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel7)
														.add(
																SignFrom_LibNameField,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												AddSignFormLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(
																SignFrom_BookNumField,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(jLabel8))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												AddSignFormLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel9)
														.add(
																SignFrom_BookLocField,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												AddSignFormLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel10)
														.add(
																SignFrom_BookConsField,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												AddSignFormLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(SignFrom_AddButton)
														.add(
																SignForm_ClearButton))
										.addContainerGap(79, Short.MAX_VALUE)));

		OkBox
				.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		OkBox.setTitle("Signature added");
		OkBox.setAlwaysOnTop(true);
		OkBox.setBounds(new java.awt.Rectangle(100, 100, 400, 90));

		OkMessage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		OkMessage
				.setText("The BookSignature was successfully added in your DataSource.");

		OkButton.setText("Thank you!");
		OkButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				OkButtonActionPerformed(evt);
			}
		});

		org.jdesktop.layout.GroupLayout OkBoxLayout = new org.jdesktop.layout.GroupLayout(
				OkBox.getContentPane());
		OkBox.getContentPane().setLayout(OkBoxLayout);
		OkBoxLayout.setHorizontalGroup(OkBoxLayout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				OkBoxLayout.createSequentialGroup().add(OkMessage,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 377,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(23, Short.MAX_VALUE)).add(
				org.jdesktop.layout.GroupLayout.TRAILING,
				OkBoxLayout.createSequentialGroup().addContainerGap(252,
						Short.MAX_VALUE).add(OkButton,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 138,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap()));
		OkBoxLayout.setVerticalGroup(OkBoxLayout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				OkBoxLayout.createSequentialGroup().add(7, 7, 7).add(OkMessage,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED).add(
								OkButton).add(16, 16, 16)));

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Bookhive Client");
		setName("MainFrame"); // NOI18N

		jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
		jLabel1.setText("Catalog Group Name:");

		groupNameField.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		groupNameField.setText("Universal-CatalogGroup");

		connectButton.setText("Connect");
		connectButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseReleased(java.awt.event.MouseEvent evt) {
				connectButtonMouseReleased(evt);
			}
		});

		MainTab.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 2, 2,
				2, new java.awt.Color(0, 0, 0)));

		AUTLabel.setText("Author:");

		TITLabel.setText("Title:");

		AUTField.setToolTipText("Author");

		TITField.setToolTipText("Author");

		searchButton.setText("Search");
		searchButton.setEnabled(false);
		searchButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				searchButtonMouseClicked(evt);
			}
		});

		LogArea.setColumns(20);
		LogArea.setRows(5);
		jScrollPane1.setViewportView(LogArea);

		resultDataList.setFont(new java.awt.Font("Monospaced", 0, 11));
		resultDataList.setModel(datalistmodel);
		resultDataList
				.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
					public void valueChanged(
							javax.swing.event.ListSelectionEvent evt) {
						resultDataListValueChanged(evt);
					}
				});
		resultDataList.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				resultDataListMouseClicked(evt);
			}
		});
		jScrollPane2.setViewportView(resultDataList);

		signsList.setFont(new java.awt.Font("Courier New", 0, 14));
		signsList.setModel(signslistmodel);
		jScrollPane3.setViewportView(signsList);

		ISBDArea.setColumns(15);
		ISBDArea.setEditable(false);
		ISBDArea.setFont(new java.awt.Font("Courier", 0, 14));
		ISBDArea.setLineWrap(true);
		ISBDArea.setRows(5);
		ISBDArea.setMargin(new java.awt.Insets(1, 4, 0, 8));
		jScrollPane4.setViewportView(ISBDArea);

		jLabel4.setText("Log");

		jLabel2.setText("Signatures");

		jLabel3.setText("ISBD");

		addSignButton.setText("Add signature");
		addSignButton.setEnabled(false);
		addSignButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				addSignButtonMouseClicked(evt);
			}
		});

		org.jdesktop.layout.GroupLayout MainTabLayout = new org.jdesktop.layout.GroupLayout(
				MainTab);
		MainTab.setLayout(MainTabLayout);
		MainTabLayout
				.setHorizontalGroup(MainTabLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								MainTabLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												MainTabLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																jScrollPane2,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																662,
																Short.MAX_VALUE)
														.add(
																jScrollPane1,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																662,
																Short.MAX_VALUE)
														.add(
																org.jdesktop.layout.GroupLayout.TRAILING,
																MainTabLayout
																		.createSequentialGroup()
																		.add(
																				MainTabLayout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								AUTLabel)
																						.add(
																								TITLabel))
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				MainTabLayout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								TITField,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								443,
																								Short.MAX_VALUE)
																						.add(
																								AUTField,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								443,
																								Short.MAX_VALUE))
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				searchButton,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				172,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
														.add(jLabel4)
														.add(
																MainTabLayout
																		.createSequentialGroup()
																		.add(
																				MainTabLayout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								jScrollPane3,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								357,
																								Short.MAX_VALUE)
																						.add(
																								MainTabLayout
																										.createSequentialGroup()
																										.add(
																												jLabel2)
																										.addPreferredGap(
																												org.jdesktop.layout.LayoutStyle.RELATED,
																												173,
																												Short.MAX_VALUE)
																										.add(
																												addSignButton,
																												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																												133,
																												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				MainTabLayout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								jLabel3)
																						.add(
																								jScrollPane4,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								299,
																								Short.MAX_VALUE))))
										.addContainerGap()));
		MainTabLayout
				.setVerticalGroup(MainTabLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								MainTabLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												MainTabLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																searchButton,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																46,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																MainTabLayout
																		.createSequentialGroup()
																		.add(
																				MainTabLayout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.BASELINE)
																						.add(
																								AUTLabel)
																						.add(
																								AUTField,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				MainTabLayout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.BASELINE)
																						.add(
																								TITLabel)
																						.add(
																								TITField,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jScrollPane2,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												251, Short.MAX_VALUE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												MainTabLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel2).add(
																jLabel3).add(
																addSignButton))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												MainTabLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.TRAILING,
																false).add(
																jScrollPane4)
														.add(jScrollPane3))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(jLabel4)
										.add(2, 2, 2)
										.add(
												jScrollPane1,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												53,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addContainerGap()));

		RDVLabel.setText("Randesvouz:");

		RDVField.setText("tcp://140.105.49.13:87");

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								org.jdesktop.layout.GroupLayout.TRAILING,
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.TRAILING)
														.add(
																org.jdesktop.layout.GroupLayout.LEADING,
																MainTab,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.add(
																org.jdesktop.layout.GroupLayout.LEADING,
																layout
																		.createSequentialGroup()
																		.add(
																				layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								jLabel1)
																						.add(
																								RDVLabel))
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								RDVField,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								392,
																								Short.MAX_VALUE)
																						.add(
																								groupNameField,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								392,
																								Short.MAX_VALUE))
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				connectButton,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				181,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.BASELINE)
																						.add(
																								jLabel1)
																						.add(
																								groupNameField,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.BASELINE)
																						.add(
																								RDVLabel)
																						.add(
																								RDVField,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
														.add(
																connectButton,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																48,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												MainTab,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addContainerGap()));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void SignFrom_AddButtonMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_SignFrom_AddButtonMouseClicked
		// TODO add your handling code here:
		AddSignForm.setEnabled(false);

		String LibId = SignFrom_LibIDField.getText();
		String LibName = SignFrom_LibNameField.getText();
		String BookNum = SignFrom_BookNumField.getText();
		String BookLoc = SignFrom_BookLocField.getText();
		String BookCon = SignFrom_BookConsField.getText();

		BookSignature newBs = null;

		if (BookCon.trim().length() < 1)
			newBs = new BookSignature(LibId, LibName, BookNum, BookLoc);
		else
			newBs = new BookSignature(LibId, LibName, BookNum, BookLoc, BookCon);

		cp.addBookSignature(currentHash, newBs);
		RecordInterface ri = this.hashData.get(currentHash);
		try {
			ri.addSignature(newBs);
			cp.addRecord(ri);
		} catch (Exception a) {
			a.printStackTrace();
		}
		OkBox.setBounds(200, 200, 450, 100);
		OkBox.setVisible(true);

	}// GEN-LAST:event_SignFrom_AddButtonMouseClicked

	private void AddSignFormWindowClosing(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_AddSignFormWindowClosing
		// TODO add your handling code here:
		this.setEnabled(true);
		AddSignForm.setVisible(false);
	}// GEN-LAST:event_AddSignFormWindowClosing

	private void addSignButtonMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_addSignButtonMouseClicked
		// TODO add your handling code here:
		AddSignForm.setBounds(0, 0, 450, 400);

		SignForm_HashArea.setText(currentHash);
		SignFrom_RecordArea.setText(hashData.get(currentHash).getISBD());
		AddSignForm.setVisible(true);
		this.setEnabled(false);

	}// GEN-LAST:event_addSignButtonMouseClicked

	private void resultDataListValueChanged(
			javax.swing.event.ListSelectionEvent evt) {// GEN-FIRST:event_resultDataListValueChanged
		// TODO add your handling code here:
		viewData();
	}// GEN-LAST:event_resultDataListValueChanged

	private void viewData() {
		if (resultDataList.getSelectedValue() == null)
			return;
		String viewRi = resultDataList.getSelectedValue().toString();
		currentHash = viewRiHash.get(viewRi);

		signslistmodel.removeAllElements();

		Iterator<BookSignature> bsIter = hashSigns.get(currentHash).iterator();
		while (bsIter.hasNext()) {
			BookSignature bs = bsIter.next();
			signslistmodel.addElement(bs.getLibraryId() + " : "
					+ bs.getLibraryName() + " - " + bs.getBookLocalization()
					+ " : " + bs.getBookNumber());
		}

		ISBDArea.setText(hashData.get(currentHash).getISBD());

	}

	private void resultDataListMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_resultDataListMouseClicked
		// TODO add your handling code here:
		viewData();
	}// GEN-LAST:event_resultDataListMouseClicked

	private void searchButtonMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_searchButtonMouseClicked
		// TODO add your handling code here:
		signslistmodel.removeAllElements();
		datalistmodel.removeAllElements();
		sourcelistmodel.removeAllElements();
		ISBDArea.setText("");

		viewRiHash.clear();

		String tit = TITField.getText();
		String aut = AUTField.getText();
		String query = "";
		if (tit.length() > 0)
			query = query + "TIT=" + tit;
		if (aut.length() > 0) {
			if (query.length() > 0)
				query = query + "&";
			query = query + "AUT=" + aut;
		}
		if (cp != null)
			cp.sendHashQuery(query);

		LogArea.append((new Date()) + " - Query sent " + query + "\n");

	}// GEN-LAST:event_searchButtonMouseClicked

	private void connectButtonMouseReleased(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_connectButtonMouseReleased
		// TODO add your handling code here:
		if (!connected) {
			String groupname = groupNameField.getText();
			String supernode = RDVField.getText();
			URI rdv = null;
			try {
				rdv = new URI(supernode);
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}

			CatalogGroupInfo gInfo = new CatalogGroupInfo(groupname, rdv);

			cp = cl.getClientForPeerGroup(groupname);
			if (cp == null && cl.createClientInGroup(gInfo))
				cp = cl.getClientForPeerGroup(groupname);

			if (cp != null) {
				MainTab.setEnabled(cp != null);
				searchButton.setEnabled(cp != null);
				addSignButton.setEnabled(cp != null);
				connected = true;
				connectButton.setText("Disconnect");
				LogArea.append((new Date()) + " - Connected to " + groupname
						+ "\n");
			} else
				LogArea.append((new Date()) + "CP is null" + "\n");

		} else {
			connected = false;
			connectButton.setText("Connect");
			searchButton.setEnabled(false);
		}

	}// GEN-LAST:event_connectButtonMouseReleased

	private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_OkButtonActionPerformed
		// TODO add your handling code here:
		OkBox.setVisible(false);
		AddSignForm.setVisible(false);
		clearAddSignForm();
		this.setEnabled(true);
	}// GEN-LAST:event_OkButtonActionPerformed

	private void SignForm_ClearButtonMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_SignForm_ClearButtonMouseClicked
		// TODO add your handling code here:
		clearAddSignForm();
	}// GEN-LAST:event_SignForm_ClearButtonMouseClicked

	public void newDataRecords(TreeSet<DataItem> dataItems) {
		LogArea.append((new Date()) + " - Received DataRecord\n");
		Iterator<DataItem> diIter = dataItems.iterator();
		while (diIter.hasNext()) {
			DataItem di = diIter.next();
			RecordInterface ri = RecordFactory.buildRecord(di.id, di.data,
					di.type, 0);
			try {
				String rihash = ri.getHash();
				RecordInterface alreadyIn = hashData.get(rihash);
				if (alreadyIn != null) {
					Vector<BookSignature> tempSigns = alreadyIn.getSignatures();
					Iterator<BookSignature> iter = tempSigns.iterator();
					while (iter.hasNext())
						ri.addSignature(iter.next());
				}

				hashData.put(ri.getHash(), ri);
				int cns = hashSources.get(ri.getHash()).size();

				String title = ri.getTitle();
				if (title.length() > 60)
					title = title.substring(0, 57) + "...";

				String authors = present(ri.getAuthors());

				String viewRi = // ri.getHash() + " - " +
				authors + " - " + title + ", " + ri.getPublicationDate() + " ("
						+ cns + ")";

				if (!datalistmodel.contains(viewRi)) {

					if (sourcelistmodel.isEmpty()) {
						datalistmodel.addElement(viewRi);
						sourcelistmodel.addElement(String.valueOf(cns));
					} else {
						boolean inserted = false;
						for (int i = 0; i < sourcelistmodel.size(); i++) {
							String nsString = sourcelistmodel.getElementAt(i)
									.toString();
							int ns = Integer.parseInt(nsString);
							if (ns > cns)
								continue;
							else {
								datalistmodel.insertElementAt(viewRi, i);
								sourcelistmodel.insertElementAt(String
										.valueOf(cns), i);
								inserted = true;
								break;
							}
						}
						if (!inserted) {
							datalistmodel.addElement(viewRi);
							sourcelistmodel.addElement(String.valueOf(cns));
						}
					}

					// resultDataList.updateUI();
				}
				viewRiHash.put(viewRi, ri.getHash());
				// viewData();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

	}

	public void newHashRecords(TreeSet<HashItem> hashItems) {

		LogArea.append((new Date()) + " - Received HashRecord\n");

		Iterator<HashItem> hashIter = hashItems.iterator();
		while (hashIter.hasNext()) {
			HashItem hi = hashIter.next();

			TreeSet<BookSignature> ts = null;
			if (hashSources.containsKey(hi.hash)) {
				ts = hashSources.get(hi.hash);
				hashSources.remove(hi.hash);
			} else {
				ts = new TreeSet<BookSignature>();
			}
			ts.addAll(hi.signs);
			hashSources.put(hi.hash, ts);

			if (hashSigns.containsKey(hi.hash)) {
				TreeSet<BookSignature> bss = hashSigns.get(hi.hash);
				Iterator<BookSignature> signsIter = hi.signs.iterator();
				while (signsIter.hasNext()) {
					BookSignature bs = signsIter.next();
					if (!(bss.contains(bs)))
						bss.add(bs);
				}
				hashSigns.remove(hi.hash);
				hashSigns.put(hi.hash, bss);
			} else {
				hashSigns.put(hi.hash, hi.signs);
			}

			if (!(hashData.containsKey(hi.hash)))
				cp.sendDataQuery(hi.hash);
			else {
				System.out.println("Record in cache");
				RecordInterface ri = hashData.get(hi.hash);
				DataItem di = new DataItem(ri);
				TreeSet<DataItem> ts1 = new TreeSet<DataItem>();
				ts1.add(di);
				newDataRecords(ts1);
			}
		}
	}

	public void closeAddSignForm() {
		this.setEnabled(true);
		AddSignForm.setVisible(false);
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		System.out.println("New version");
		// ClientFrame.printSysEnvVar();
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new ClientFrame().setVisible(true);
			}
		});
	}

	private String present(Vector<String> authors) {
		StringBuffer ret = new StringBuffer();
		int count = 0;

		Iterator<String> iterAut = authors.iterator();
		while (iterAut.hasNext()) {
			String aut = iterAut.next();
			count++;
			String[] split = aut.split(",");
			for (int i = 0; i < split.length; i++) {
				ret.append(split[i].trim());
				if (i < split.length - 1)
					ret.append(" ");
			}
			if (iterAut.hasNext())
				ret = ret.append(", ");
			if (count == 2)
				break;
		}
		if (iterAut.hasNext())
			ret.append("...");

		return ret.toString();
	}

	private void clearAddSignForm() {

		SignFrom_BookConsField.setText("");
		SignFrom_BookLocField.setText("");
		SignFrom_BookNumField.setText("");
		SignFrom_LibIDField.setText("");
		SignFrom_LibNameField.setText("");
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JTextField AUTField;

	private javax.swing.JLabel AUTLabel;

	private javax.swing.JFrame AddSignForm;

	private javax.swing.JTextArea ISBDArea;

	private javax.swing.JTextArea LogArea;

	private javax.swing.JPanel MainTab;

	private javax.swing.JDialog OkBox;

	private javax.swing.JButton OkButton;

	private javax.swing.JLabel OkMessage;

	private javax.swing.JTextField RDVField;

	private javax.swing.JLabel RDVLabel;

	private javax.swing.JButton SignForm_ClearButton;

	private javax.swing.JTextField SignForm_HashArea;

	private javax.swing.JButton SignFrom_AddButton;

	private javax.swing.JTextField SignFrom_BookConsField;

	private javax.swing.JTextField SignFrom_BookLocField;

	private javax.swing.JTextField SignFrom_BookNumField;

	private javax.swing.JTextField SignFrom_LibIDField;

	private javax.swing.JTextField SignFrom_LibNameField;

	private javax.swing.JTextArea SignFrom_RecordArea;

	private javax.swing.JTextField TITField;

	private javax.swing.JLabel TITLabel;

	private javax.swing.JButton addSignButton;

	private javax.swing.JButton connectButton;

	private javax.swing.JTextField groupNameField;

	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel10;

	private javax.swing.JLabel jLabel2;

	private javax.swing.JLabel jLabel3;

	private javax.swing.JLabel jLabel4;

	private javax.swing.JLabel jLabel5;

	private javax.swing.JLabel jLabel6;

	private javax.swing.JLabel jLabel7;

	private javax.swing.JLabel jLabel8;

	private javax.swing.JLabel jLabel9;

	private javax.swing.JScrollPane jScrollPane1;

	private javax.swing.JScrollPane jScrollPane2;

	private javax.swing.JScrollPane jScrollPane3;

	private javax.swing.JScrollPane jScrollPane4;

	private javax.swing.JScrollPane recordArea;

	private javax.swing.JList resultDataList;

	private javax.swing.JButton searchButton;

	private javax.swing.JList signsList;
	// End of variables declaration//GEN-END:variables

}
